'''Now playing module.'''
############################################################################
#    Copyright (C) 2006 by Robert Small                                    #
#    SmallR2002@Gmail.com                                                  #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

__module_name__        = "bcenpea"
__module_version__     = "0.0.1"
__module_description__ = "Now playing module."

import xchat
import sys
import os
import commands
import string #IGNORE:W0402

if os.name == 'posix':
    import dbus

if (sys.path.count(os.path.dirname(__file__)) == 0):
    sys.path.append(os.path.dirname(__file__))

#  Pretty much essential.
import bcsettings

def now_playing(word, word_eol, userdata): #IGNORE:W0613
    '''
    Run through now playing alternatives and use one.
    @param word: all the data you need to work with, chopped up into small parts.
    @type word: string
    @param word_eol: all the data you need to work with, chopped from wherever you're starting from.
    @type word_eol: string
    @param userdata: custom object to pass back.
    @type userdata: object
    '''
    ans = "" #Satisfy code tests
    
    for w in bcsettings.players:
        exec("ans = np_" + w + "()") #IGNORE:W0122
        
        if ans != None:
            xchat.command("ACTION is currently playing: " + ans)
            return xchat.EAT_ALL
    #If we've got this far we have  slight issue...
    xchat.prnt("Not detected anything playing...")

def amarok(word, word_eol, userdata): #IGNORE:W0613
    '''
    Amarok now playing.
    @param word: all the data you need to work with, chopped up into small parts.
    @type word: string
    @param word_eol: all the data you need to work with, chopped from wherever you're starting from.
    @type word_eol: string
    @param userdata: custom object to pass back.
    @type userdata: object
    '''
    ans  = np_amarok()
    
    if ans != None:
        xchat.command("ACTION is currently playing: " + ans)
        return xchat.EAT_ALL
    xchat.prnt("Not detected anything playing...")

def np_amarok():
    '''
    Get nowplaying string or return None
    '''
    if os.name != 'posix':
        r = "Please read the docs, the Amarok dbus interface is POSIX only."
        return r
    
    if commands.getoutput('dcop amarok player title') == None:
        return None
    else:
        r = string.replace(bcsettings.np_output, "%ti", commands.getoutput('dcop amarok player title'))
        r = string.replace(r, "%ar", commands.getoutput('dcop amarok player artist'))
        r = string.replace(r, "%al", commands.getoutput('dcop amarok player album'))
        r = string.replace(r, "%tr", commands.getoutput('dcop amarok player track'))
        r = string.replace(r, "%yr", commands.getoutput('dcop amarok player year'))
        return r

def exaile(word, word_eol, userdata): #IGNORE:W0613
    '''
    Exaile now playing.
    @param word: all the data you need to work with, chopped up into small parts.
    @type word: string
    @param word_eol: all the data you need to work with, chopped from wherever you're starting from.
    @type word_eol: string
    @param userdata: custom object to pass back.
    @type userdata: object
    '''
    ans  = np_exaile()()
    
    if ans != None:
        xchat.command("ACTION is currently playing: " + ans)
        return xchat.EAT_ALL
    xchat.prnt("Not detected anything playing...")

def np_exaile():
    '''
    Get nowplaying string or return None
    '''
    #Cut non-posix users out now.
    if os.name != 'posix':
        r = "Please read the docs, the Exaile dbus interface is POSIX only."
        return r
    
    #Ugly line of code to get a dbus connection to Exaile.
    try:
        dbus_interface = dbus.Interface(dbus.SessionBus().get_object("org.exaile.DBusInterface","/DBusInterfaceObject"),"org.exaile.DBusInterface")
    except: 
        #FIXME: Needs condition.
        #Fail, return none and pretend we did noting...
        return None #IGNORE:W0702
    
    #We now have a connection, let's do business.
    r = string.replace(bcsettings.np_output, "%ti", dbus_interface.get_title())
    r = string.replace(bcsettings.np_output, "%ar", dbus_interface.get_artist())
    r = string.replace(bcsettings.np_output, "%al", dbus_interface.get_album())
    #r = string.replace(bcsettings.np_output, "%tr", dbus_interface.something)
    #r = string.replace(bcsettings.np_output, "%yr", dbus_interface.something)
    
    return r

def rhythmbox(word, word_eol, userdata): #IGNORE:W0613
    '''
    Rhythmbox now playing.
    @param word: all the data you need to work with, chopped up into small parts.
    @type word: string
    @param word_eol: all the data you need to work with, chopped from wherever you're starting from.
    @type word_eol: string
    @param userdata: custom object to pass back.
    @type userdata: object
    '''
    ans  = np_rhythmbox()
    
    if ans != None:
        xchat.command("ACTION is currently playing: " + ans)
        return xchat.EAT_ALL
    xchat.prnt("Not detected anything playing...")

def np_rhythmbox():
    '''
    Get nowplaying string or return None
    '''
    #Cut non-posix users out now.
    if os.name != 'posix':
        r = "Please read the docs, the Exaile dbus interface is POSIX only."
        return r
    
    t = bcsettings.np_output
    t = t.replace("%ti", "%tt")
    t = t.replace("%ar", "%ta")
    t = t.replace("%al", "%at")
    t = t.replace("%tr", "%tn")
    t = t.replace("%yr", "%ay")
    r = commands.getoutput('rhythmbox-client --print-playing-format "' + t + '"')
    if r.count("Rhythmbox-WARNING") > 0:
        return None
    return r

def np_kaffeine():
    '''
    Get nowplaying string or return None
    '''
    return None

def np_vlc():
    '''
    Get nowplaying string or return None
    '''
    return None

def np_miro():
    '''
    Get nowplaying string or return None
    '''
    return None

def np_kmplayer():
    '''
    Get nowplaying string or return None
    '''
    return None

def np_bmp():
    '''
    Get nowplaying string or return None
    '''
    return None

def np_mpd():
    '''
    Get nowplaying string or return None
    '''
    return None